import { observable, action } from 'mobx';
import { message } from 'antd';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { ValidateForm } from './util';
import * as Api from '../../apis/setting/mailBlackList';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
const CONDITION = () => ({
    conditioninfo: [
      {
        defaultshow: true,
        items: [
          {
            colSpan: 1,
            conditionType: 'INPUT',
            domkey: ['mailAddress'],
            fieldcol: 24,
            isQuickSearch: false,
            label: getLabel('508418', '邮箱地址或域名后缀'),
            labelID: 508418,
            labelcol: 0,
            precision: 0,
            relatekey: '',
            viewAttr: 3,
            otherParams: {
              hideLabel: true,
            },
            rules: 'required|string',
          },
        ],
      },
    ],
  });
const initalState = {
  editVisible: false,
  editFields: [],
};
export default class BlackListStore {
  @observable blackTable = new TableStore();
  @observable editForm = new WeaForm();
  @observable editVisible = initalState.editVisible;
  @observable editFields = initalState.editFields;

  @action setStoreState = (params) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action clear = () => this.setStoreState(initalState);

  @action getMailBlackList = (params, initPage = false) => {
    Api.getMailBlackList(params).then((res) => {
      initPage ? this.blackTable.getDatas(res.sessionkey) :
      this.blackTable.getDatas(res.sessionkey, 1);
    });
  }

  @action initalEditForm = (callback) => {
    this.editForm.initFormFields(CONDITION().conditioninfo);
    this.editFields = CONDITION().conditioninfo;
    if (callback) callback();
  }

  @action editSubmit = (callback) => {
    ValidateForm(this.editForm, () => {
      let formParams = this.editForm.getFormParams();
      let params = { ...formParams, operation: 'add' };
      Api.operateUserEmailBlack(params).then((res) => {
        if (res.flag) {
          message.success(getLabel(83551, '保存成功！'));
          this.getMailBlackList('', true);
        } else {
          message.error(res.message || getLabel(21809, '保存失败，请重试！'));
        }
        if (callback) callback();
      });
    });
  }

  @action operateUserEmailBlack = async (params) => {
    let res = await Api.operateUserEmailBlack(params);
    return res;
  }
}
